// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System;
using System.Reflection;
using NUnit.Framework;
using ValidationAspects;
using ValidationAspects.Exceptions;
using ValidationAspects.Sdk;

namespace TestValidationAspects
{
	public static class TestHelpers
	{
		public static void ResetFramework()
		{
			TypeValidationRegistry.ClearCache();
			PropertyValidationRegistry.ClearCache();
			ParameterValidationRegistry.ClearCache();
			MethodValidationRegistry.ClearCache();

			ValidationSettings.StateValidateProperties = true;
			ValidationSettings.InterceptValidateProperties = true;

			ValidateGreaterThanException.ResetMessageFormat();
			ValidateInRangeException.ResetMessageFormat();
			ValidateIsDateException.ResetMessageFormat();
			ValidateIsEmailException.ResetMessageFormat();
			ValidateLengthInRangeException.ResetMessageFormat();
			ValidateLessThanException.ResetMessageFormat();
			ValidateMatchesRegexException.ResetMessageFormat();
			ValidateMaximumException.ResetMessageFormat();
			ValidateMaximumLengthException.ResetMessageFormat();
			ValidateMinimumException.ResetMessageFormat();
			ValidateMinimumLengthException.ResetMessageFormat();
			ValidateNotEqualException.ResetMessageFormat();
			ValidateNotInRangeException.ResetMessageFormat();
			ValidateNotNullException.ResetMessageFormat();
			ValidateNotNullOrEmptyException.ResetMessageFormat();
			ValidateObjectException.ResetMessageFormat();
		}

		public static void TestValues(this IValidationContext context, object instance, Type instanceType, PropertyInfo property, MethodBase method, ParameterInfo parameter, object[] methodArgs)
		{
			Enforce.IsNotNull(context, "context");

			Assert.AreSame(instance, context.Instance, "instance does not match");
			Assert.AreSame(instanceType, context.Type, "instanceType does not match");
			Assert.AreSame(property, context.Property, "property does not match");
			Assert.AreSame(method, context.Method, "method does not match");
			Assert.AreSame(parameter, context.Parameter, "parameter does not match");

			if (methodArgs == null)
			{
				Assert.IsNull(context.Arguments);
				return;
			}

			if (context.Arguments == null)
			{
				Assert.IsNull(methodArgs);
				return;
			}

			Assert.AreEqual(methodArgs.Length, context.Arguments.Length);

			for (int i = 0; i < methodArgs.Length; ++i)
			{
				Assert.AreEqual(methodArgs[i], context.Arguments[i]);
			}
		}
	}
}